<template>
  <el-dialog
    v-el-drag-dialog
    :title="type === 'add' ? '新增商品型号' : '编辑商品型号'"
    :visible.sync="visible"
    width="500px"
    @closed="reset"
  >
    <el-form ref="dataForm" label-width="60px" class="form" :model="temp" :rules="rules">
      <el-form-item label="编码:" prop="specAliasCode">
        <el-input v-model="temp.specAliasCode" placeholder="请输入" clearable>
          <el-button v-if="type==='add'" slot="append" class="append-search" @click.prevent="getCode">获取</el-button>
        </el-input>
      </el-form-item>
      <el-form-item label="规格:" prop="specAlias">
        <el-input v-model="temp.specAlias" :maxlength="20" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item label="备注:" prop="remark" class="block">
        <el-input v-model="temp.remark" type="textarea" :rows="5" :maxlength="1000" show-word-limit />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">
        关闭
      </el-button>
      <el-button :loading="$loadingPlugin.add || $loadingPlugin.edit" type="primary" @click="success">
        保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { queryOrgCode, goodsSpecAliasAdd } from '@/api'

  export default {
    name: 'add',
    data() {
      return {
        visible: false,
        type: 'add',
        temp: {
          specAliasId: 0,
          specAliasCode: '',
          specAlias: '',
          remark: ''
        },
        rules: {
          specAlias: [{ required: true, message: '请输入规格', trigger: 'blur' }]
        }
      }
    },
    methods: {
      open(temp) {
        return new Promise(resolve => {
          this.resolve = resolve
          this.visible = true
          if (temp) {
            this.temp = Object.assign(this.temp, temp)
            this.type = 'edit'
          }
        })
      },
      // 获取商品型号编码
      async getCode() {
        const res = await queryOrgCode({
          codeType: 6
        })
        this.temp.specAliasCode = res.data
      },
      success() {
        this.$refs.dataForm.validate(valid => {
          if (!valid) return
          const params = {
            specAliasId: this.temp.specAliasId,
            specAliasCode: this.temp.specAliasCode,
            specAlias: this.temp.specAlias,
            remark: this.temp.remark
          }
          if (this.type === 'add') {
            this.add(params)
          } else {
            this.edit(params)
          }
        })
      },
      async add(params) {
        await goodsSpecAliasAdd(params)
        this.resolve()
        this.visible = false
        this.$notify({
          title: '提示',
          message: '新增成功',
          type: 'success',
          duration: 2000
        })
      },
      async edit(params) {
        await goodsSpecAliasAdd(params)
        this.resolve(this.temp)
        this.visible = false
        this.$notify({
          title: '提示',
          message: '更新成功',
          type: 'success',
          duration: 2000
        })
      },
      reset() {
        this.type = 'add'
        this.temp = {
          specAliasId: 0,
          specAliasCode: '',
          specAlias: '',
          remark: ''
        }
        this.$nextTick(() => {
          this.$refs.dataForm.clearValidate()
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
.form {
  display: flex;
  flex-wrap: wrap;
  & /deep/ .el-form-item {
    width: 100%;
    padding-right: 10px;
    .el-form-item__content > .el-select {
      width: 100%;
    }
  }
  .block {
    width: 100%;
  }
}
.info-title {
  display: flex;
  align-items: center;
  margin-top: 10px;
  margin-bottom: 20px;
  .title-icon {
    display: inline-block;
    width: 4px;
    height: 18px;
    margin-right: 6px;
    background: $themeColor;
  }
  span {
    font-weight: 700;
    color: $themeColor;
  }
}
.char {
  p {
    margin: 5px 0;
  }
}
</style>
